package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.AddressModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class AddressLeftAdapter extends ItemAdapter<AddressModel.DatasBean, AddressLeftAdapter.DefaultHolder> {


    public AddressLeftAdapter(Context context) {
        super(context);
    }

    private int selectPoi = -1;

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_address_left, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        AddressModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.poi = position;
        holder.txtProvince.setText(item.name);
        if (selectPoi == position) {
            holder.txtProvince.setTextColor(0xffFF0030);
        } else {
            holder.txtProvince.setTextColor(0xff000000);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.txt_province)
        TextView txtProvince;
        AddressModel.DatasBean item;
        private int poi;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            txtProvince.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            selectPoi = poi;
            notifyDataSetChanged();
            if (onChooseListener != null) {
                onChooseListener.onProvinceSelect(item.id, item.name);
            }
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onProvinceSelect(String id, String name);
    }
}
